package action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import service.ICustomerService;
import vo.Customer;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@SuppressWarnings("serial")
public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{
	private int page;
	private int rows;
	
	private ICustomerService customerService;
	public Customer customer=new Customer();
	
	
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	
	public Customer getCustomer() {
		return customer;
	}
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	
	//=================前台返回Json对象============================
		
		//查找所有的学生信息并分页
	
		//========================================================
	
    
	
	
	public ICustomerService getCustomerService() {
		return customerService;
	}
	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}
	
   //====================1.显示所有商品=============================== 
    public String showCustomer() throws Exception{
    	String hql=createHql();
    	List customer1=customerService.getCustomer(page, rows,hql);
    	int total=customerService.getAllCustomer(hql);
    	
    	Map<String,Object>  map=new HashMap<String,Object>();
    	map.put("total", total);
    	map.put("rows", customer1);
    	writeJson(map);  
    	
    	 customer=new Customer();
    	
    	
    	return null;
    	
    }
    
	
	//===================2.添加商品==========================================
    public String addCustomer(){
    	
    	
      Result result = new Result();
        try {
        	customerService.addCustomer(customer);
            result.setSuccess(true);
            result.setMsg("添加商品成功");
          
        } catch (Exception e) {
            result.setMsg("添加商品失败");
        }
        writeJson(result);
        customer=new Customer();
		
   	
   	return null;
    	
    }
    
	
	//======3.删除商品============
    public String delCustomer(){
    	
    	
    	 Result result = new Result();
         try {
        	 customerService.delCustomer(customer.getCustomerId());
             result.setSuccess(true);
             result.setMsg("成功删除一条记录");
           
         } catch (Exception e) {
             result.setMsg("删除失败");
         }
         writeJson(result);
         customer=new Customer();
		
    	
    	return null;
    	
    }
	
	//======4.编辑商品============
    public void editCustomer(){
		
	    	
	      Result result = new Result();
	        try {
	        	 customerService.editCustomer(customer);
	            result.setSuccess(true);
	            result.setMsg("成功修改一条商品记录");
	          
	        } catch (Exception e) {
	            result.setMsg("删除失败");
	        }
	        writeJson(result);
	        customer=new Customer();
	        
			
	   	
	   
	    	
		
	}
	
	//======5.查询商品============
	
	//======6.获得所有的customer============
    //========7.1向前台输出json的writeJson方法====================
    public void writeJson(Object object) {
        try {         
        	JSONObject jsonObjectFromMap = JSONObject.fromObject(object);
        
        	String out=jsonObjectFromMap.toString();
          
            ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
            ServletActionContext.getResponse().getWriter().write(out);
            ServletActionContext.getResponse().getWriter().flush();
        } catch (IOException e) {
        	//customerService.delCustomer(customer.getCustomerId());
            }
     
    }
    //========7.2获得查询的hql语句====================
    public String createHql(){
    	
    	
    	
    	
    	String hql="from Customer p where 1=1 ";
    
    	
    	
		
	
    		System.out.println("customerId"+hql);
			if(customer.getCustomerId()!=null)
				hql+=" and p.customerId="+customer.getCustomerId();
			
			System.out.println("customerName"+hql);
				if(customer.getCustomerName()!=null)
				hql+=" and p.customerName like '%"+customer.getCustomerName()+"%'";
		
			System.out.println("customerSex"+hql);
			if(customer.getCustomerSex()!=null)
				hql+=" and p.customerSex like '%"+customer.getCustomerSex()+"%'";
			
			System.out.println("customerTel"+hql);
			if(customer.getCustomerTel()!=null)
				hql+=" and p.customerTel="+customer.getCustomerTel();
			
			System.out.println("customerClass"+hql);
			if(customer.getCustomerClass()!=null)
				hql+=" and p.customerClass="+customer.getCustomerClass();
			
		
    	return hql;
    	
    }
	public Customer getModel() {
		
		return customer;
	}
	
    
}
